import {Snake} from "./Snake";
import {Food} from "./Food";
import {ScorePanel} from "./ScorePanel";

/**
 * 游戏控制器
 */
export class GameControl {

    /**
     * 蛇类
     * @private
     */
    private _snake: Snake;

    /**
     * 食物类
     * @private
     */
    private _food: Food;

    /**
     * 计分类
     * @private
     */
    private _scorePanel: ScorePanel;

    /**
     * 初始速度
     * @private
     */
    private readonly _initSpeed: number = 100;

    /**
     * 每级提升的速度
     * @private
     */
    private readonly _levelSpeed: number = 30;

    /**
     * 最高等级
     * @private
     */
    private readonly _maxLevel: number = 10;

    /**
     * 每次移动的步长
     * @private
     */
    private readonly _step: number = 10;

    /**
     * 移动方向
     * @private
     */
    private _direction: string = "ArrowRight";

    /**
     * 是否暂停
     * @private
     */
    private _isPause: boolean = false;

    /**
     * 开始按钮
     * @private
     */
    private readonly _start = document.getElementById('start')!;

    /**
     * 继续按钮
     * @private
     */
    private readonly _continueEl = document.getElementById('continue')!;

    /**
     * 暂停按钮
     * @private
     */
    private readonly _pause = document.getElementById('pause')!;

    /**
     * 再来一次按钮
     * @private
     */
    private readonly _again = document.getElementById('again')!;

    constructor() {
        this._snake = new Snake();
        this._food = new Food();
        this._scorePanel = new ScorePanel(this._maxLevel, this._step);
        this._start.addEventListener("click", () => this.start());
        this._continueEl.addEventListener("click", () => this.continue());
        this._pause.addEventListener("click", () => this.pause());
        this._again.addEventListener("click", () => location.reload());
    }

    /**
     * 开始
     */
    public start(): void {
        this._start.setAttribute("class", "none");
        this._pause.setAttribute("class", "show");
        document.addEventListener("keydown", ev => this._direction = ev.key);
        this.move();
    }

    /**
     * 暂停
     */
    public pause(): void {
        this._isPause = true;
        this._pause.setAttribute("class", "none");
        this._continueEl.setAttribute("class", "show");
    }

    /**
     * 继续
     */
    public continue(): void {
        this._isPause = false;
        this._continueEl.setAttribute("class", "none");
        this._pause.setAttribute("class", "show");
        this.move();
    }

    /**
     * 是否死亡
     */
    public die(): boolean{
        let live = this._snake.isLive;
        if (!live) {
            alert("GAME OVER");
            this._again.setAttribute("class", "show");
            this._continueEl.setAttribute("class", "none");
            this._pause.setAttribute("class", "none");
            this._start.setAttribute("class", "none");
            return true;
        }
        return false;
    }

    /**
     * 移动
     * @private
     */
    private move(): void {
        this._snake.move(this._direction, this._step, this.touchingFood.bind(this));
        !this.die() && !this._isPause && setTimeout(this.move.bind(this), this._initSpeed - this._levelSpeed * (this._scorePanel.level - 1));
    }

    /**
     * 是否吃到食物
     * @param x     当前蛇头x坐标
     * @param y     当前蛇头y坐标
     * @private
     */
    private touchingFood(x: number, y: number): void {
        if (x === this._food.x && y === this._food.y) {
            this._scorePanel.addScore();
            this._snake.lengthen();
            this._food.change();
        }
    }

}